<?php
include 'class/in_it.php';
login_redirect();
include 'includes/overall/header.php';   

if (empty($_POST) === false){
    $required_fields = array('username', 'password', 'r_password', 'firstname', 'lastname', 'gender', 'emailaddress');
    foreach($_POST as $key=>$value){
        if (empty($value) && in_array($key, $required_fields) === true){
            $errors[] = 'Please fill all fields.';
            break 1;
        }
    }
    
    if (empty($errors) === true){
        if (user_exists($_POST['username']) === true){
            $errors[] = 'Username \'' . $_POST['username'] . '\' already exist.';
        }
        if (preg_match("/\\s/", $_POST['username']) == true){
            $errors[] = 'Your username must not contain any spaces.';
        }
        if (strlen($_POST['password']) < 6){
            $errors[] = 'Your password must be atleast 6 characters.';
        }
        if ($_POST['password'] !== $_POST['r_password']){
            $errors[] = 'Your password do not match.';
        }
        if (email_exists($_POST['emailaddress'])=== true){
            $errors[] = 'Sorry, the email \'' . $_POST['emailaddress'] . '\' is already in use.';
        }
    } 
}


?>
<h1>Register</h1>

<?php
if (isset($_GET['success']) && empty($_GET['success'])){
    echo 'You\'ve been registered successfully! Thank You for registering. ';
}   else{

if (empty($_POST) === false && empty($errors) === true){
    $register_data = array (
        'username' => $_POST['username'],
        'password' => $_POST['password'],
        'firstname' => $_POST['firstname'],
        'lastname' => $_POST['lastname'],
        'gender' => $_POST['gender'],
        'emailaddress' => $_POST['emailaddress'],
    );
    
    register_user($register_data);
    header('Location: register.php?success');
    exit();//exit
    

}       else if (empty($errors) === false){
        echo output_errors($errors);
    }
?>

<form action="" method="post">
    <ul>
        <li>
            Username:<br>
            <input type="text" name="username">
        </li>
        <li>
            Password:<br>
            <input type="password" name="password">
        </li>
        <li>
            Repeat Password:<br>
            <input type="password" name="r_password">
        </li>
        <li>
            First name:<br>
            <input type="text" name="firstname">
        </li>
        <li>
            Last name:<br>
            <input type="text" name="lastname">
        </li>
        <li>
            Gender:<br>
            <input type="radio" name="gender" value="male">Male
            <input type="radio" name="gender" value="female">Female
        </li>
        <li>
            Email Address:<br>
            <input type="text" name="emailaddress">
        </li>
        <li>    
            <input type="submit" value="register">
        </li>
    </ul>
</form>
<?php } include 'includes/overall/footer.php'; ?>
